Zepto(function ($) {

  window.onload=function () {
    /**
     * 配置微信config
     */
    getInitConfig();
    /**
     * 获取订单信息
     */
    confirmOrderMsg();
  }
  var ShopCartsIdList = sessionStorage.getItem("ShopCartsIdList");
  $("#region").val("微信");
  /**
   * /wxmall/order/confirmOrder
   */
  function confirmOrderMsg(){
    var a = JSON.parse(ShopCartsIdList);
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/order/confirmOrder',
      data: JSON.stringify(a),
      dataType: 'json',
      timeout: 15000,
      headers: headers("order.confirmOrder"),
      contentType:'application/json',
      success: function(data){
        disposeget(data);
      },
      error: function(xhr, type){
      }
    });
  }
  var name = "";
  var region = "";
  var address = "";
  var mobile = "";
  var idCard = "";
  function disposeget(data) {
    console.log(data);
    var myDate = new Date();
    console.log(myDate.getSeconds());
    if(!data.success){
      return
    }
    var data1 = data.data;
    var defaultAddress = data1.defaultAddress;
    var goodslist = data1.orderGoodsList;
    var calculate = data1.calculate;
    $("#totalPrice")[0].innerHTML ="￥" + calculate.totalPrice;
    $("#postMoney")[0].innerHTML ="￥" + calculate.postMoney;
    $("#totalDiscountPrice")[0].innerHTML ="￥" + calculate.totalDiscountPrice;
    $("#payMoney")[0].innerHTML ="￥" + calculate.payMoney;
    var a = sessionStorage.getItem("comfir");
    if(a == "select"){
      name = sessionStorage.getItem("comfirmName");
      mobile = sessionStorage.getItem("comfirmMobile");
      region = sessionStorage.getItem("comfirmRegion");
      address = sessionStorage.getItem("comfirmDetailed");
      sessionStorage.setItem("comfir","1");
    }else {
      name = defaultAddress.name;
      region = defaultAddress.region;
      address = defaultAddress.detailed;
      mobile = defaultAddress.mobile;
      idCard = defaultAddress.idCard
    }
    $(".order-name")[0].innerHTML = name;
    $(".address-phone")[0].innerHTML = mobile;
    $(".address")[0].innerHTML = region + address;
    // 商品列表
    $("#goodList").html("");
    if(goodslist.length > 0 ){
      var html = "";
      for(var j = 0; j< goodslist.length ; j++){
        var picUrl = "";
        if(goodslist[j].picUrl != null&& goodslist[j].picUrl != undefined){
          picUrl = imgUrl + goodslist[j].picUrl;
        }
        var productName = "";
        if(goodslist[j].productName != null&& goodslist[j].productName != undefined){
          productName = goodslist[j].productName;
        }
        var specification = "";
        if(goodslist[j].specification != null&& goodslist[j].specification != undefined){
          specification = goodslist[j].specification;
        }
        var price = "";
        if(goodslist[j].price != null&& goodslist[j].price != undefined){
          price ="￥" + goodslist[j].price;
        }
        var total_money = "";
        if(goodslist[j].total_money != null&& goodslist[j].total_money != undefined){
          total_money ="￥" + goodslist[j].total_money;
        }
        var num = "";
        if(goodslist[j].num != null&& goodslist[j].num != undefined){
          num = goodslist[j].num;
        }
        html +='<div class="goods-list">\n' +
            '                            <div class="left"><img src="'+picUrl+'" /></div>\n' +
            '                            <div class="center">\n' +
            '                                <div class="center-content">\n' +
            '                                    <div class="goods-desc">'+productName+'</div>\n' +
            '                                    <div class="goods-specification">规格：'+specification+'</div>\n' +
            '                                </div>\n' +
            '                            </div>\n' +
            '                            <div class="right">\n' +
            '                                <div class="goods-prise"><span class="z-price">'+price+'</span><span class="s-price">'+total_money+'</span></div>\n' +
            '                                <div class="goods-num"></div>\n' +
            '                                <div class="select-num-content">\n' +
            '                                    <div class="add-num">-</div>\n' +
            '                                    <div class="value-num">'+num+'</div>\n' +
            '                                    <div class="add-add">+</div>\n' +
            '                                </div>\n' +
            '                            </div>\n' +
            '                        </div>';
      }
      $("#goodList").append(html);
    }
  }
  /**
   * 返回上一页
   */
  $(".nav-header").on("click", ".back", function (e) {
      window.history.go(-1);
  });
  /**
   * 选择支付方式
   *
   */
  // $("#select").click(function () {
  //   $(".popur").css("display","block");
  // });
  // $(".cancle").click(function () {
  //   $("#region").val("微信");
  //   $(".popur").css("display","none");
  // });
  // $(".cancle-binbing").click(function () {
  //   $("#region").val("支付宝");
  //   $(".popur").css("display","none");
  // });
  // $(".ocpice").click(function () {
  //   $(".popur").css("display","none");
  // });
  /**
   * 选择地址
   *
   */
  $(".order-types").click(function () {
    sessionStorage.setItem("comfir","select");
    window.location.href="/usercenter/addressmanage/address.html"
  });
  /**
   * 确认下单
   *
   */
  $("#topay").click(function () {
    createOrder();
    // getorderlist();
  });

  function getorderlist() {
    var data = {
      "totalFee": "0.01",
      "outTradeNo":"20171019173239980993"
    };
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/pay/prepay',
      data: JSON.stringify(data),
      dataType: 'json',
      timeout: 15000,
      headers: headers("pay.prepay"),
      contentType:'application/json',
      success: function(data){
        if(data.success)
          preinvokes(data.data);
        else
          alert(resp.msg);
      },
      error: function(xhr, type){
        alert(JSON.stringify(xhr));
        alert(JSON.stringify(type));
      }
    });
  }
  /**
   * 选择优惠券
   */
  $("#showCityPicker").click(function () {
    window.location.href="/usercenter/discount.html";
  });
  $(".add-tocart").click(function () {
    window.history.go(-1);
  });
  function createOrder() {
    var a = JSON.parse(ShopCartsIdList);
    var data = {
      "name":name,
      "region":region,
      "address":address,
      "mobile":mobile,
      "phone":"",
      "idcard": idCard,
      "selectShopCartsIdList":a.unique3()
    }
    $.ajax({
      type: 'POST',
      url: getUrl + '/wxmall/order/createOrder',
      data: JSON.stringify(data),
      dataType: 'json',
      timeout: 15000,
      headers: headers("order.createOrder"),
      contentType:'application/json',
      success: function(data){
        dispose(data);
      },
      error: function(xhr, type){
      }
    });
  }
  function dispose(data) {
    if(data.success){
      mui.toast(data.msg);
      console.log(data);
      var data1 = data.data;
      var arr = data1.split(":");
      sessionStorage.setItem("zOrderInfoId",arr[0]);
      window.location.href="/usercenter/order/waitPaymentDetails.html"
    }else {
      mui.toast(data.msg);
    }
  }

  Array.prototype.unique3 = function(){
    var res = [];
    var json = {};
    for(var i = 0; i < this.length; i++){
      if(!json[this[i]]){
        res.push(this[i]);
        json[this[i]] = 1;
      }
    }
    return res;
  }
});